﻿using System;
using System.Collections.Generic;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Users;

namespace Web.Cms
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                HttpCookie UserCookies = Request.Cookies["UserName"];
                HttpCookie Counts = Request.Cookies["counter"];
                if (UserCookies != null)
                {
                    if (ConvertUtility.ToInt32(Counts.Value) > 3)
                    {
                        btLogin.Enabled = false;
                    }
                }
            }

        }
        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);

            if (IsPostBack)
            {
                HttpCookie CookieUser = Request.Cookies["UserName"];
                HttpCookie Count = Request.Cookies["counter"];

                if (CookieUser != null)
                {
                    int count = ConvertUtility.ToInt32(Count.Value);
                    if (count >= 3)
                    {
                        lblMessage.Text = "Tài khoản bị khóa trong vòng 15p";
                        lblMessage.Visible = true;
                        btLogin.Enabled = false;
                        Response.Cookies["UserName"].Expires = DateTime.Now.AddMinutes(14);
                        Response.Cookies["counter"].Expires = DateTime.Now.AddMinutes(14);
                        //Response.Redirect(Request.RawUrl);
                    }
                    else
                    {
                        lblMessage.Text = "Thông tin không chính xác";
                        lblMessage.Visible = true;
                    }
                }
            }
        }
        public int _counter;
        protected void btLogin_Click(object sender, EventArgs e)
        {

            try
            {
                string returnUrl = Request.QueryString["returnUrl"];
                if (UserController.Login(txtUsername.Text.Trim(), SecurityUtility.MD5Encrypt(txtPassword.Text.Trim()), chkRememberPwd.Checked))
                {
                    UserInfo _userInfo = UserController.GetUser(txtUsername.Text.Trim());
                    AuditController.CreateAuditQ(0, (int)AuditCode.Login, "", _userInfo.UserID, 0, "", 1, DateTime.Now);
                    if (returnUrl != null)
                        Response.Redirect(returnUrl);
                    else
                        Response.Redirect(UIProcess.DEFAULT_CP_PAGE);
                }
                else
                {
                    HttpCookie UserCookies = Request.Cookies["UserName"];
                    HttpCookie Counts = Request.Cookies["counter"];
                    if (UserCookies != null)
                    {
                        _counter = ConvertUtility.ToInt32(Counts.Value);
                        _counter++;

                        HttpCookie UserCookie = new HttpCookie("UserName", txtUsername.Text);
                        HttpCookie Count = new HttpCookie("counter", _counter.ToString());
                        Response.Cookies.Set(UserCookie);
                        Response.Cookies.Set(Count);
                    }
                    else
                    {
                        _counter++;
                        HttpCookie UserCookie = new HttpCookie("UserName", txtUsername.Text);
                        HttpCookie Count = new HttpCookie("counter", _counter.ToString());
                        Response.Cookies.Set(UserCookie);
                        Response.Cookies.Set(Count);
                    }
                }
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }
    }
}